/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if (root)
            q.push(root);
        while (!q.empty())
        {
            int size = q.size();
            vector<int> tmp;
            while (size--)
            {
                TreeNode* front = q.front();
                q.pop();
                
                if (front->left)
                    q.push(front->left);
                if (front->right)
                q.push(front->right);

                tmp.push_back(front->val);
            }
            
            ret.push_back(tmp);
        }

        return ret;
    }
private:
    vector<vector<int>> ret;
    queue<TreeNode*> q;
};